﻿using HookupAppForm.Dto;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HookupAppForm
{
    public partial class FormShowUser : Form
    {
        private string url = "api/services/app/User/";

        private APIClient Client { get; set; }

        FormMain FormMain;
        public FormShowUser(FormMain _formMain, APIClient _client)
        {
            FormMain = _formMain;
            InitializeComponent();
            Client = _client;
        }


        private void FormShowUser_Load(object sender, EventArgs e)
        {
            string act = string.Format(url + "GetAll");
            var result = Client.Get(act);
            if(Utils.ExceptionHandler(result) == null)
            {
                ItemsDto<UserDto> res = JsonConvert.DeserializeObject<ItemsDto<UserDto>>(result);

                comboBox1.DataSource = res.result.items;
                comboBox1.DisplayMember = "UserName";
                comboBox1.ValueMember = "UserName";
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            
            var item = comboBox1.SelectedItem as UserDto;
            FormMain.UserEmail = item.EmailAddress;
            FormMain.UserEmailName = item.UserName;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        
        }
    }
}
